package com.jy.admin.rest;

import com.jy.admin.annotations.AdminSignIgnore;
import com.jy.admin.auth.anno.VisitorAccessible;
import com.jy.admin.db.dao.SmsSendLogDao;
import com.jy.admin.db.entity.SmsSendLogEntity;
import com.jy.admin.enums.SmsLogStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2021/12/11
 * Time: 11:44 上午
 */

@Controller
public class ShortLinkController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShortLinkController.class);
    @Autowired
    private SmsSendLogDao smsSendLogDao;

    @AdminSignIgnore
    @RequestMapping("/shot/{tag}")
    @VisitorAccessible
    public String shotLinkRedirect(@PathVariable String tag) {

        LOGGER.info("== 点击进来啦 ==,tag:{}", tag);
        String jumpUrl = "https://play.kyyy.in";
        SmsSendLogEntity smsSendLogEntity = smsSendLogDao.selectByTag(tag);
        if (smsSendLogEntity != null) {
            LOGGER.info("== 点击进来啦 找到tag==,tag:{}", tag);
            smsSendLogEntity.setStatus(SmsLogStatusEnum.clicked.getStatus());
            smsSendLogDao.updateByPrimaryKey(smsSendLogEntity);
            jumpUrl = smsSendLogEntity.getJumpUrl();
        }else{
            LOGGER.info("== 点击进来啦 找不到tag==,tag:{}", tag);
        }
        return "redirect:" + jumpUrl;
    }

    @AdminSignIgnore
    @RequestMapping("/shot/test")
    @VisitorAccessible
    public String test(HttpServletRequest request) {

        String jumpUrl = "https://play.kyyy.in";
        return "redirect:" + jumpUrl;
    }
}
